package com.bsj.sipgateway.web.service.impl;

import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.dto.ClassificationDTO;
import com.bsj.power.common.def.entity.Classification;
import com.bsj.power.common.def.vo.ClassificationSceneDevicesVO;
import com.bsj.power.common.def.vo.ClassificationTreeVO;
import com.bsj.power.common.def.vo.SceneLevelVO;
import com.bsj.power.common.mapper.ClassificationMapper;
import com.bsj.sipgateway.web.service.ClassificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.instrument.ClassFileTransformer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhaoyang
 * @Date 2024/9/4 14:33
 */
@Service("classificationServiceImpl")
public class ClassificationServiceImpl implements ClassificationService {

    @Autowired
    private ClassificationMapper classificationMapper;


    @Override
    public JsonResult add(ClassificationDTO classificationDTO) {
        Integer biggestId = classificationMapper.selectBiggestId();

        Classification classification = new Classification();
        BeanUtils.copyProperties(classificationDTO, classification);
        // 主键不是自增的，获取当前数据库最大主键值，+1
        if (biggestId == null) {
            classification.setId(1);
        } else {
            classification.setId(biggestId+ 1);
        }
        // 没有pid，默认是最上级，层级是1，顺序是1
        if (classification.getPid() == null) {
            classification.setPid(0);
            classification.setLayer(1);
            classification.setSort(1);

        } else {
            // 设置层级和顺序，根据pid获取上级数据，层级就是上级的层级+1，顺序是上级的第一级子级的顺序的最大值+1
            Classification classificationParent = classificationMapper.selectById(classification.getPid());
            classification.setLayer(classificationParent.getLayer() + 1);
            // 查询父级的下一级，设置顺序
            List<Classification> classificationChild = classificationMapper.selectByPid(classification.getPid());
            if (classificationChild.size() == 0) {
                classification.setSort(1);
            } else {
                int maxSort = classificationChild.stream().mapToInt(Classification::getSort).max().getAsInt();
                classification.setSort(maxSort + 1);
            }

        }
        classificationMapper.insert(classification);
        return JsonResult.success();
    }

    @Override
    public Classification queryById(Integer id) {

        return null;
    }

    @Override
    public JsonResult getTree() {
        // 查询所有
        List<Classification> classificationList = classificationMapper.selectList(null);
        List<ClassificationTreeVO> vos = new ArrayList<>();
        for (Classification classification : classificationList) {
            ClassificationTreeVO vo = new ClassificationTreeVO();
            BeanUtils.copyProperties(classification, vo);
            vos.add(vo);
        }

        List<ClassificationTreeVO> treeVOS = buildTree(0, vos);
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setData(treeVOS);
        return jsonResult;
    }

    @Override
    public JsonResult classificationSceneDevices() {
        // 先查询所有分类，展示层级结构
        List<Classification> classificationList = classificationMapper.selectList(null);
        List<ClassificationSceneDevicesVO> vos = new ArrayList<>();
        for (Classification classification : classificationList) {
            ClassificationSceneDevicesVO vo = new ClassificationSceneDevicesVO();
            BeanUtils.copyProperties(classification, vo);
            vos.add(vo);
        }

        List<ClassificationSceneDevicesVO> bigTreeVOS = buildBigTree(0, vos);


        return null;
    }

    private static List<ClassificationSceneDevicesVO> buildBigTree(int parentId, List<ClassificationSceneDevicesVO> nodes) {
        List<ClassificationSceneDevicesVO> rootNodes = new ArrayList<>();
        for (ClassificationSceneDevicesVO node : nodes) {
            if (node.getPid() == parentId) {
                List<ClassificationSceneDevicesVO> child = buildBigTree(node.getId(), nodes);
                // child按照sort排序
                if (child.size() != 0) {
                    List<ClassificationSceneDevicesVO> sortedChild = child.stream()
                            .sorted(Comparator.comparing(ClassificationSceneDevicesVO::getSort)).collect(Collectors.toList());
                    node.setChild(sortedChild);
                } else {
                    node.setChild(child);
                }
                rootNodes.add(node);
            }
        }
        return rootNodes;
    }


    private static List<ClassificationTreeVO> buildTree(int parentId, List<ClassificationTreeVO> nodes) {
        List<ClassificationTreeVO> rootNodes = new ArrayList<>();
        for (ClassificationTreeVO node : nodes) {
            if (node.getPid() == parentId) {
                List<ClassificationTreeVO> child = buildTree(node.getId(), nodes);
                // child按照sort排序
                if (child.size() != 0) {
                    List<ClassificationTreeVO> sortedChild = child.stream()
                            .sorted(Comparator.comparing(ClassificationTreeVO::getSort)).collect(Collectors.toList());
                    node.setChild(sortedChild);
                } else {
                    node.setChild(child);
                }
                rootNodes.add(node);
            }
        }
        return rootNodes;
    }


}
